//
//  GGStatus_One_CollectionViewCell.m
//  XTXOneToOne
//
//  Created by Canny on 2018/11/15.
//  Copyright © 2018年 WK. All rights reserved.
//

#import "GGStatus_One_CollectionViewCell.h"
#import "GGStatus_OneTabCell.h"

@interface GGStatus_One_CollectionViewCell ()
<
UITableViewDelegate,
UITableViewDataSource
>

@property(nonatomic, strong)ExceptionView * exceptionView;
@property (nonatomic,assign)NSInteger GGpage_Status;
@property (nonatomic,strong)NSString * order_sn;

@end

@implementation GGStatus_One_CollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self){
        [self initView];
    }
    return self;
}


- (void)initView{
    
    self.backgroundColor=RGB(231,231,231);
    
    [self setProperty];
    
    [self createUI];
    
    [self refreashData];
    
    
}


#pragma mark==================setProperty======================
-(void)setProperty{
    
    self.backgroundColor=RGB(231, 231, 231);
    _GGpage_Status = 1;
    //    _dataMarray=[[NSMutableArray alloc]init];
}

#pragma mark==================createUI======================
-(void)createUI{
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self refreashData];
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    [self addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(0);
        make.left.equalTo(self.mas_left).offset(0);
        make.right.equalTo(self.mas_right).offset(0);
        make.bottom.equalTo(self.mas_bottom).offset(0);
    }];
    
}

#pragma mark-下拉刷新
- (void)refreashData{
    [self loadGGDataType:GGOrderSlideView_DoingType_RefrechData];
}


-(void)loadMoreData{
    
    [self loadGGDataType:GGOrderSlideView_DoingType_loadMoreData];

    
}

-(void)relayOutUI{
    WS(wself);
    GGOrderModel * Amodel = [GGOrderModel sharedGGOrderModel];
    if (Amodel.GG_Status_1_Marray.count<1) {
        
        [self.tableView reloadData];
        
        
        self.exceptionView = [ExceptionView exceptionViewWithType:ExceptionCodeTypeNoData];
        self.exceptionView.heightH = G_SCREENHEIGHT-kStatusBarAndNavigationBarHeight-kTabBarHeight;
        
        [self.exceptionView showInView:wself.tableView];
    }else{
        [self.exceptionView dismiss];
        self.exceptionView = nil;
        [self.tableView reloadData];
    }
    
}

#pragma mark tableView 代理相关
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    GGOrderModel * Amodel = [GGOrderModel sharedGGOrderModel];
    return Amodel.GG_Status_1_Marray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0001;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [GGStatus_OneTabCell computeHeight:@""];
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WS(weakSelf);
    NSString *reuseIdentifier =[NSString stringWithFormat:@"GGStatus_OneTabCell-%ld-%ld",(long)indexPath.section,(long)indexPath.row];
    GGStatus_OneTabCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[GGStatus_OneTabCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.block = ^(NSString * _Nonnull order_sn) {
        weakSelf.order_sn = order_sn;
        
        //判断登录
        
        [weakSelf openAlert:@"确定撤单?" andTag:10002];
    };
    GGOrderModel * Amodel = [GGOrderModel sharedGGOrderModel];
    
    if (Amodel.GG_Status_1_Marray.count>0) {
        cell.dataSource=Amodel.GG_Status_1_Marray[indexPath.row];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    
}



-(void)loadGGDataType:(GGOrderSlideView_DoingType)Type{

//    page    整型    否    页数
//    limit    整型    否    每页显示的数量
//    type    整型    否    订单类型 @1:限价买单 @2:限价卖单
//    pair_id    整型    否    交易对id
//    status    整型    否    订单状态    @1:委托订单 @2:平仓当单 @3:历史订单
    NSString * URL = [NSString stringWithFormat:@"%@ex/leverorder/getOrder",DEFAULTSEVERBASEURL];
    if (Type == GGOrderSlideView_DoingType_RefrechData) {
        GGOrderModel * GGModel = [GGOrderModel sharedGGOrderModel];
        [GGModel.GG_Status_1_Marray removeAllObjects];
        _GGpage_Status=1;
    }else if (Type == GGOrderSlideView_DoingType_loadMoreData){
        _GGpage_Status++;
    }

    NSDictionary * Dic = @{
                           @"page":[NSString stringWithFormat:@"%ld",_GGpage_Status],
                           @"access_token":[LoginModel GetValue:DL_token],
                           @"limit":[NSString stringWithFormat:@"%d",GGListNum],
                           @"status":@"1"
                           };
    
    WS(weakSelf);
    
    [HUD showProgress:@"" inView:self];
    
    
    [MOLoadHttpsData GetHttpDataWithUrlStr:URL Dic:Dic SuccessBlock:^(id  _Nonnull responseObject) {
        
        [HUD dismissInView:weakSelf];
        
        NSDictionary * dic = responseObject;
        
        NSString * code = [NSString stringWithFormat:@"%@",dic[@"code"]];
        
        if ([code isEqualToString:@"0"]) {
            NSDictionary * dicc = dic[@"data"];
            
            GGOrderModel * GGModel = [GGOrderModel sharedGGOrderModel];
            
            NSMutableArray * marr = [[NSMutableArray alloc]init];
            for (NSDictionary *temp in dicc[@"list"]) {
                LTOrderCellModel * Model = [LTOrderCellModel mj_objectWithKeyValues:temp];
                [marr addObject:Model];
            }
            
            [GGModel.GG_Status_1_Marray addObjectsFromArray:marr];
            if (marr.count==0||marr.count<GGListNum) {
                weakSelf.tableView.mj_footer.hidden=YES;
            }else{
                weakSelf.tableView.mj_footer.hidden=NO;
            }
            [weakSelf relayOutUI];
        }
        
        [weakSelf.tableView.mj_header  endRefreshing];
        [weakSelf.tableView.mj_footer  endRefreshing];

    } FailureBlock:^(id  _Nonnull error) {
        [weakSelf.tableView.mj_header  endRefreshing];
        [weakSelf.tableView.mj_footer  endRefreshing];
      
    }];
    
    
}


-(void)openAlert:(NSString *)title andTag:(NSInteger)tag{
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:title delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alert.delegate=self;
    alert.tag = tag;
    alert.alertViewStyle = UIAlertViewStyleDefault;
    [alert show];
    
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    if (buttonIndex == 1) { //确认
        [self Http_leverorder_cancelorder:self.order_sn];
    }
    
}


/*

1、接口地址

请求方式：post
请求接口：leverorder/cancelorder
是否登录：是
2、接口请求参数

参数名    参数类型    是否必须    含义    备注
order_sn    字符串    是    订单号
*/
-(void)Http_leverorder_cancelorder:(NSString *)order_sn{
    NSString * URL = [NSString stringWithFormat:@"%@leverorder/cancelorder",DEFAULTSEVERBASEURL];
    NSDictionary * Dic = @{
                           @"order_sn":order_sn,
                           @"access_token":[LoginModel GetValue:DL_token]
                           };
    WS(weakSelf);
    [HUD showProgress:@"" inView:self];
    [MOLoadHttpsData PostHttpDataWithUrlStr:URL Dic:Dic SuccessBlock:^(id  _Nonnull responseObject) {
        [HUD dismissInView:weakSelf];
        NSDictionary * dic = responseObject;
        
        NSString * code = [NSString stringWithFormat:@"%@",dic[@"code"]];
        NSString * msg = [NSString stringWithFormat:@"%@",dic[@"msg"]];
        if ([code isEqualToString:@"0"]) {
            [HUD showSuccessMessage:msg];
            [weakSelf refreashData];
        }else{
            [HUD showErrorMessage:msg];
        }
        
    } FailureBlock:^(id  _Nonnull error) {
        [HUD dismissInView:weakSelf];
    }];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end


